﻿
IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[sproc_GetAllChildClass]') AND type in (N'P', N'PC'))
    DROP PROCEDURE [dbo].[sproc_GetAllChildClass];
GO
CREATE PROCEDURE [dbo].[sproc_GetAllChildClass] 
    @Class_id int
/*

========================================================
功能:    得到该类的所有子类
参数:
    @Class_id int        :    组（类）ID
备注:可以优化
========================================================

*/
AS
BEGIN
SET NOCOUNT ON;

with Class_Subtree ( ClassID )
as
(
    select ClassID from dbo.UDS_Class where ClassParentID = @class_id and ClassParentID <> ClassID

    union all

    select c.ClassID
    from dbo.UDS_Class c inner join Class_Subtree s on c.ClassParentID = s.ClassID and c.ClassParentID <> c.ClassID
)
select * 
from dbo.UDS_Class
where ClassID in ( select ClassID from Class_Subtree );

END
GO